

#include "stdafx.h"
#include "pf_constructionline.h"

#include "pf_debug.h"



/**
 * Constructor.
 */
PF_ConstructionLine::PF_ConstructionLine(PF_Container* parent,
        const PF_ConstructionLineAttribute& d)
        :PF_AtomicEntity(parent), data(d) {

    calculateBorders();
}



/**
 * Destructor.
 */
PF_ConstructionLine::~PF_ConstructionLine() {}



PF_Entity* PF_ConstructionLine::clone() {
    PF_ConstructionLine* c = new PF_ConstructionLine(*this);
    c->initId();
    return c;
}



void PF_ConstructionLine::calculateBorders() {
    minV = PF_Vector::minimum(data.point1, data.point2);
    maxV = PF_Vector::maximum(data.point1, data.point2);
}



PF_Vector PF_ConstructionLine::getNearestEndpoint(const PF_Vector& coord,
        double* dist) {
    double dist1, dist2;
    PF_Vector* nearerPoint;

    dist1 = data.point1.distanceTo(coord);
    dist2 = data.point2.distanceTo(coord);

    if (dist2<dist1) {
        if (dist!=NULL) {
            *dist = dist2;
        }
        nearerPoint = &data.point2;
    } else {
        if (dist!=NULL) {
            *dist = dist1;
        }
        nearerPoint = &data.point1;
    }

    return *nearerPoint;
}



PF_Vector PF_ConstructionLine::getNearestPointOnEntity(const PF_Vector& coord,
        bool /*onEntity*/, double* /*dist*/, PF_Entity** entity) {

    if (entity!=NULL) {
        *entity = this;
    }

    PF_Vector ae = data.point2-data.point1;
    PF_Vector ea = data.point1-data.point2;
    PF_Vector ap = coord-data.point1;
    PF_Vector ep = coord-data.point2;
	
	if (ae.magnitude()<1.0e-6 || ea.magnitude()<1.0e-6) {
		return PF_Vector(false);
	}

    // Orthogonal projection from both sides:
    PF_Vector ba = ae * PF_Vector::dotP(ae, ap)
                   / (ae.magnitude()*ae.magnitude());
    PF_Vector be = ea * PF_Vector::dotP(ea, ep)
                   / (ea.magnitude()*ea.magnitude());

    return data.point1+ba;
}



PF_Vector PF_ConstructionLine::getNearestCenter(const PF_Vector& /*coord*/,
        double* dist) {

    if (dist!=NULL) {
        *dist = LX_DOUBLEMAX;
    }

    return PF_Vector(false);
}



PF_Vector PF_ConstructionLine::getNearestMiddle(const PF_Vector& /*coord*/,
        double* dist) {
    if (dist!=NULL) {
        *dist = LX_DOUBLEMAX;
    }
    return PF_Vector(false);
}



PF_Vector PF_ConstructionLine::getNearestDist(double /*distance*/,
        const PF_Vector& /*coord*/,
        double* dist) {
    if (dist!=NULL) {
        *dist = LX_DOUBLEMAX;
    }
    return PF_Vector(false);
}


double PF_ConstructionLine::getDistanceToPoint(const PF_Vector& coord,
        PF_Entity** entity,
        LX_Define::ResolveLevel /*level*/, double /*solidDist*/) {

    PF_DEBUG->print("PF_ConstructionLine::getDistanceToPoint");

    if (entity!=NULL) {
        *entity = this;
    }
    double dist = LX_DOUBLEMAX;
    PF_Vector ae = data.point2-data.point1;
    PF_Vector ea = data.point1-data.point2;
    PF_Vector ap = coord-data.point1;
    PF_Vector ep = coord-data.point2;
	
	if (ae.magnitude()<1.0e-6 || ea.magnitude()<1.0e-6) {
		return dist;
	}

    // Orthogonal projection from both sides:
    PF_Vector ba = ae * PF_Vector::dotP(ae, ap) /
                   PF_Math::pow(ae.magnitude(), 2);
    PF_Vector be = ea * PF_Vector::dotP(ea, ep) /
                   PF_Math::pow(ea.magnitude(), 2);

    PF_DEBUG->print("ba: %f", ba.magnitude());
    PF_DEBUG->print("ae: %f", ae.magnitude());

    PF_Vector cp = PF_Vector::crossP(ap, ae);
    dist = cp.magnitude() / ae.magnitude();

    return dist;
}



void PF_ConstructionLine::move(PF_Vector offset) {
    data.point1.move(offset);
    data.point2.move(offset);
    //calculateBorders();
}



void PF_ConstructionLine::rotate(PF_Vector center, double angle) {
    data.point1.rotate(center, angle);
    data.point2.rotate(center, angle);
    //calculateBorders();
}



void PF_ConstructionLine::scale(PF_Vector center, PF_Vector factor) {
    data.point1.scale(center, factor);
    data.point2.scale(center, factor);
    //calculateBorders();
}



void PF_ConstructionLine::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
	data.point1.mirror(axisPoint1, axisPoint2);
	data.point2.mirror(axisPoint1, axisPoint2);
}



/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_ConstructionLine& l) {
    os << " ConstructionLine: " << l.getData() << "\n";
    return os;
}


